#include "analysplots.h"
#include "ui_analysplots.h"
#include "qcustomplot.h"
#include "defs.h"
#include "manager.h"

#include <QStandardItemModel>

const int MAX_COUNT = 216; // 18*12=216
const int monthesInYear = 12;

int monthesFromYear(const double years)
{
    return round( years * monthesInYear );
}

AnalysPlots::AnalysPlots(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AnalysPlots)
{
    ui->setupUi(this);
}

AnalysPlots::~AnalysPlots()
{
    delete ui;
}

void AnalysPlots::test()
{
    // generate some data:
    QVector<double> age(MAX_COUNT), stegnoD(MAX_COUNT), stegnoH(MAX_COUNT);

    int opCount = App->pacient()->operationsCount();
    const QStandardItemModel* operations = App->pacient()->operations();

    for (int i = 0; i < opCount; ++i)
    {
        age[i] = monthesFromYear( operations->index(i, IC_Age).data().toDouble() );
        stegnoD[i] = operations->index(i, IC_UStegno).data().toDouble();
        stegnoH[i] = operations->index(i, IC_ZStegno).data().toDouble();
    }

    ui->plots->addGraph();
    ui->plots->graph(0)->setPen(QPen(Qt::blue));
    ui->plots->graph(0)->setData(age, stegnoD);
    ui->plots->graph(0)->setName("Здорове");

    ui->plots->addGraph();
    ui->plots->graph(1)->setPen(QPen(Qt::red));
    ui->plots->graph(1)->setData(age, stegnoH);
    ui->plots->graph(0)->setName("Хворе");

    // give the axes some labels:
    ui->plots->xAxis->setLabel("Вік (міс.)");
    ui->plots->yAxis->setLabel("Стегно (см)");
    // set axes ranges, so we see all data:
    ui->plots->xAxis->setRange(-1, 1);
    ui->plots->yAxis->setRange(0, 1);
    ui->plots->replot();
}
